#include <p32xxxxx.h>
#include <xlcd.h>

#define _XTAL_FREQ 4000000
#pragma config BOR = ON,BORV = 3,VREGEN = OFF,WDT = OFF,WDTPS = 32768,CCP2MX = ON,PBADEN = ON      // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF,MCLRE = ON ,STVREN = ON,LVP = OFF,ICPRT = OFF,XINST = OFF
int decenas,unidades,recepcion,u2;
int s1;
int Alarm;
float sensor1;
void DelayFor18TCY(){__delay_ms(20);}
void DelayPORXLCD(){__delay_ms(15);}
void DelayXLCD(){__delay_ms(5);}

int dec=3;
int uni=5;

void main(){
    ADCON1=13;  ADCON2=140; TRISD=0;
    OpenXLCD(FOUR_BIT & LINES_5X7); ADCON1=14; ADCON2=140;

 
    while (1){

        ADCON0=3; while (ADCON0==3);
        sensor1=ADRESH*256+ADRESL;
        sensor1=sensor1*500/1024;
        s1=(int)sensor1;
        decenas=(int)sensor1/10;
      
        if((decenas==dec && unidades==uni) )
        {SetDDRamAddr(0x00);putrsXLCD("S");
        PORTDbits.RD0=1;}
        if((decenas!=dec && unidades!=uni ))
        {SetDDRamAddr(0x00);putrsXLCD("N");
        PORTDbits.RD0=0;}
        if(decenas==dec1 && unidades==uni1)
        {SetDDRamAddr(0x00);putrsXLCD("S");
        PORTDbits.RD0=1;}
        if(decenas!=dec1 && unidades!=uni1 )
        {SetDDRamAddr(0x00);putrsXLCD("N");
        PORTDbits.RD0=0;}

        unidades=(int)sensor1%10;
        ADCON0=7; while (ADCON0==7);

        SetDDRamAddr(0x01);
        WriteDataXLCD(decenas+0x30);
        SetDDRamAddr(0x02);
        WriteDataXLCD(unidades+0x30);
       
        if(u2<=1)
        {SetDDRamAddr(0x40);putrsXLCD("B");
        if(Alarm==1){PORTDbits.RD0=1;}
        }
    }
  
}


